local addonName, IWonew = ...

AutoCraftClass = {}
AutoCraftClass.__index = AutoCraftClass

function AutoCraftClass:new()
  local instance = setmetatable({}, AutoCraftClass)
  instance.CurrentEnchantingRecipeID = nil
  instance.Create_DH_OBJECT_COUNT = 1
  instance.firstCheckRegents = false
  instance.use_jingmaihuihuang_recipeID = 1
	return instance
end

function AutoCraftClass:loop(type)
  if self.firstCheckRegents == false then
    self:GetRemainRegents(true)
    self.firstCheckRegents = true
  end
  if type == Enum.Profession.Blacksmithing then
    local result = self:CreateBlacksmithing()
    if result then
      self:GetRemainRegents(false)
      IWonew.CreateTextFrame(80, "锻C", 400, 0)
      print("锻造制作完成！")
    end
  end
  
  if type == Enum.Profession.Enchanting then
    local result = self:CreateEnchanting()
    if result then
      self:GetRemainRegents(false)
      IWonew.CreateTextFrame(80, "附D", 480, 0)
      print("附魔制作完成！")
    end
  end
end

function AutoCraftClass:reset()
  self.done = false
  self.CurrentEnchantingRecipeID = nil
end


-- 获取碎裂精华buff
function AutoCraftClass:SetSLJHBuff()
  local itemID = 213610
  local itemSlot = IWonew.GetItemSlot(itemID)
  if itemSlot == nil then return end
  local recipeSpellID = 445466
  -- C_TradeSkillUI.CraftSalvage(recipeSpellID, numCasts?, itemTarget [, craftingReagents [, applyConcentration]])
  -- 晶脉粉末的位置
  C_TradeSkillUI.CraftSalvage(recipeSpellID, 1, itemSlot.itemLocation)
end


function AutoCraftClass:CreateBlacksmithing()
  IWonew.Temp_Vars.needCraftLog = true
  local skillLineID = 2872 -- 锻造skillLineID
  local currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(skillLineID)
  local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyType)
  local recipeID = 450219 -- 镔爪合金配方ID
  -- 配方所需的材料
  local craftingReagents = {
    {dataSlotIndex = 1, itemID = 222418, quantity = 2}, -- 2星心核合金
    {dataSlotIndex = 2, itemID = 210937, quantity = 5}, -- 2星镔爪框
  }

  local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(recipeID, craftingReagents, IWonew.getRandomString(10), false)
  if operationInfo and operationInfo.concentrationCost < currencyInfo.quantity then
    -- C_TradeSkillUI.CraftRecipe(recipeSpellID [, numCasts [, craftingReagents [, recipeLevel [, orderID [, applyConcentration]]]]])
    C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, nil, true)
    print("能使用专注制作")
    return false
  end
  print("不能使用专注制作")
  return true
end

function AutoCraftClass:CreateEnchanting()
  IWonew.Temp_Vars.needCraftLog = true
  local skillLineID = 2874 -- 附魔skillLineID
  local currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(skillLineID)
  local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyType)
  -- 获取附魔羊皮纸位置
  local itemSlot = IWonew.GetItemSlot(38682)
  if itemSlot == nil then
    print("没有附魔羊皮纸")
    return true
  end
  if self.CurrentEnchantingRecipeID == nil then
    print("没有可用的附魔配方")
    return true
  end
  -- 配方所需的材料
  local craftingReagents = self:GetRecipeCraftingReagents(self.CurrentEnchantingRecipeID)
  if not craftingReagents then
    print("没有可用的附魔配方----材料")
    return true
  end

  local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(self.CurrentEnchantingRecipeID, craftingReagents, IWonew.getRandomString(10), false)
  if operationInfo and operationInfo.concentrationCost < currencyInfo.quantity then
    -- C_TradeSkillUI.CraftEnchant(recipeSpellID, numCasts, craftingReagents, itemTarget, applyConcentration) end
    C_TradeSkillUI.CraftEnchant(self.CurrentEnchantingRecipeID, 1, craftingReagents, itemSlot.itemLocation, true)
    print("能使用专注制作")
    return false
  end
  print("不能使用专注制作")
  return true
end

function AutoCraftClass:GetReLiuTieZhanItemCount()
  local items = IWonew.GetAllBagItems()
  local numSet = {0}
  for i = 1, #items do
    if items[i].itemName == "热流铁砧" then
      local tooltip = C_TooltipInfo.GetBagItem(items[i].bagID, items[i].slotID)
      if tooltip.lines[3] and tooltip.lines[3].leftText then
        local num = string.match(tooltip.lines[3].leftText, "%d+")
        table.insert(numSet, tonumber(num))
      end
    end
  end
  local sum = 0
  for _, v in ipairs(numSet) do
    sum = sum + v
  end
  return sum
end

function AutoCraftClass:GetRemainRegents(isFirstCheck)
  local dz_skillLineID = 2872 -- 锻造skillLineID
  local dz_currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(dz_skillLineID)
  local dz_currencyInfo = C_CurrencyInfo.GetCurrencyInfo(dz_currencyType)

  local fm_skillLineID = 2874 -- 附魔skillLineID
  local fm_currencyType = C_TradeSkillUI.GetConcentrationCurrencyID(fm_skillLineID)
  local fm_currencyInfo = C_CurrencyInfo.GetCurrencyInfo(fm_currencyType)

  local currentRole, currentRealm = UnitFullName("player")
  -- 212306=奇思合计 38682=附魔羊皮纸 213610=晶脉粉末 224765=点火装置 223692=晶脉辉煌3星 222428=镔爪合金3星 223790=诅咒急速3星
  -- 163036=宠物币
  local itemIDs = {38682, 213610, 212306, 224765, 223692, 222428, 223790, 163036}
  
  local allItems = IWonew.GetAllBagItems()
  local reliuCount = self:GetReLiuTieZhanItemCount()
  local resultString = "role=" .. currentRole .. "&realm=" .. currentRealm
  if isFirstCheck then
    resultString = resultString .. "&firstCheck=1"
  end
  for j = 1, #itemIDs do
    local itemID = itemIDs[j]
    local stackCount = 0
    for i = 1, #allItems do
      if allItems[i].itemID == itemID then
        stackCount = stackCount + allItems[i].stackCount
      end
    end
    resultString = resultString .. "&itemID_" .. itemID .. "=" .. stackCount
  end
  resultString = resultString .. "&itemID_668=" .. dz_currencyInfo.quantity
  resultString = resultString .. "&itemID_669=" .. fm_currencyInfo.quantity
  resultString = resultString .. "&itemID_87216=" .. reliuCount
  IWonew.Log("CHECK_REAGENTS----" .. "{" .. resultString .. "}")

end

-- 制造点火装置
function AutoCraftClass:CreateDhObject()
  if self.Create_DH_OBJECT_COUNT >= 10 then
    -- IWonew.CreateTextFrame(80, "点A", 560, 0)
    IWonew.CreateTextFrame(80, "拆E", 560, 0)
    return
  end
  local currentCount = 0
  local dhItemSlot = IWonew.GetItemSlot(224765)
  if dhItemSlot then
    currentCount = dhItemSlot.stackCount
  end
  if currentCount >= 1 then
    -- IWonew.CreateTextFrame(80, "点A", 560, 0)
    IWonew.CreateTextFrame(80, "拆E", 560, 0)
    return
  end
  local recipeID = 453727 -- 点燃点火装置配方ID
  local craftingReagents = {
    {dataSlotIndex = 1, itemID = 222418, quantity = 1}, -- 2星心核合金
  }
  C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, nil, false)
  self.Create_DH_OBJECT_COUNT = self.Create_DH_OBJECT_COUNT + 1
end


function AutoCraftClass:CheckBuffs()
  self:CheckDHBuff()
  self:CheckQSHJBuff()
end

function AutoCraftClass:CheckDHBuff()
  if self.CheckDHBuff_Frame then
    self.CheckDHBuff_Frame:UnregisterAllEvents()
    self.CheckDHBuff_Frame:ClearAllPoints()
    self.CheckDHBuff_Frame:Hide()
    self.CheckDHBuff_Frame = nil  -- 将框架设置为 nil，允许垃圾回收器清理内存
  end
  local currentTime = GetTime()
  -- 判断锻造是否学习
  -- /dump C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
  -- local professionInfo = C_TradeSkillUI.GetProfessionInfoByRecipeID(453727)
  -- print("professionInfo---", professionInfo)
  -- print(IWonew.TableToString(professionInfo))
  -- -- 锻造技能未学习或者技能点小于50不使用点火装置
  -- if professionInfo == nil or professionInfo.skillLevel < 50 then return end
  local skillLineID = 2872 -- 锻造skillLineID
	local configID = C_ProfSpecs.GetConfigIDForSkillLine(skillLineID)
	local pathID = 99267 -- 恒燃熔炉pathID
  -- {99267, 99266, 99265, 99264} 4个节点
	local nodeInfo = C_Traits.GetNodeInfo(configID, pathID)
  local activeRank = (nodeInfo and nodeInfo.activeRank or 0) - 1
  -- print("activeRank", activeRank, "pathID:", pathID)

  if activeRank < 10 then return end
	-- print("nodeInfo", IWonew.TableToString(nodeInfo))
	-- local description = C_ProfSpecs.GetDescriptionForPath(pathID)
	-- print("description:---", description)
  local aura = C_UnitAuras.GetPlayerAuraBySpellID(453734)
  if aura and ((aura.expirationTime - currentTime) > 2 * 60 ) then return end
  self.CheckDHBuff_Frame = IWonew.CreateTextFrame(80, "点火", 80, 0)
end

function AutoCraftClass:CheckQSHJBuff()
  if self.CheckQSHJBuff_Frame then
    self.CheckQSHJBuff_Frame:UnregisterAllEvents()
    self.CheckQSHJBuff_Frame:ClearAllPoints()
    self.CheckQSHJBuff_Frame:Hide()
    self.CheckQSHJBuff_Frame = nil  -- 将框架设置为 nil，允许垃圾回收器清理内存
  end
  local currentTime = GetTime()
  local aura = C_UnitAuras.GetPlayerAuraBySpellID(432306)
  if aura and ((aura.expirationTime - currentTime) > 3 * 60 ) then return end
  self.CheckQSHJBuff_Frame = IWonew.CreateTextFrame(80, "奇思", 160, 0)
end

function AutoCraftClass:SetAvailableEnchantingRecipe()
  local zuzhoujisu_recipeID = 445388 -- 诅咒急速专注配方ID
  local jingmaihuihuang_recipeID = 445333 -- 晶脉辉煌配方ID
  --if (self.use_jingmaihuihuang_recipeID == 1) and self:IsRecipeLearned(jingmaihuihuang_recipeID) then
    --self.CurrentEnchantingRecipeID = jingmaihuihuang_recipeID
   -- return
  --end
  if (self.use_jingmaihuihuang_recipeID == 0) and self:IsRecipeLearned(zuzhoujisu_recipeID) then
    self.CurrentEnchantingRecipeID = zuzhoujisu_recipeID
    return
  end
  self.CurrentEnchantingRecipeID = jingmaihuihuang_recipeID
  
  
end

function AutoCraftClass:IsRecipeLearned(recipeID)
  local recipeInfo = C_TradeSkillUI.GetRecipeInfo(recipeID)
  -- print("recipeInfo", IWonew.TableToString(recipeInfo))
	if recipeInfo and recipeInfo.learned then
    return true
  end
  return false
end

function AutoCraftClass:GetRecipeCraftingReagents(recipeID)
  if recipeID == 445333 then
    -- 晶脉辉煌配方所需的材料
    return {
      {dataSlotIndex = 1, itemID = 219947, quantity = 75}, -- 2星风暴之尘
      {dataSlotIndex = 2, itemID = 219950, quantity = 10}, -- 2星粼光碎片
      {dataSlotIndex = 3, itemID = 219955, quantity = 1}, -- 2星耀辉水晶
      -- {dataSlotIndex = 3, itemID = 219954, quantity = 1}, -- 2星耀辉水晶
      -- {dataSlotIndex = 4, itemID = 222500, quantity = 1}, -- 2精铸框架
    }
  end
  if recipeID == 445388 then
    -- 诅咒急速配方所需的材料
    return {
      {dataSlotIndex = 1, itemID = 219947, quantity = 50}, -- 2星风暴之尘
      {dataSlotIndex = 2, itemID = 219950, quantity = 10}, -- 2星粼光碎片
      {dataSlotIndex = 3, itemID = 219954, quantity = 1}, -- 2星耀辉水晶
     -- {dataSlotIndex = 4, itemID = 222500, quantity = 1}, -- 2精铸框架
    }
  end
  return nil
end

local AutoCraft_instance = nil

function CreateAutoCraftInstance()
  if AutoCraft_instance == nil then
    AutoCraft_instance = AutoCraftClass:new()
  end
  return AutoCraft_instance
end


function CreateDhObject()
  CreateAutoCraftInstance():CreateDhObject()
end

function CheckBuffs()
  CreateAutoCraftInstance():CheckBuffs()
end

function Exe_DXT(type)
  CreateAutoCraftInstance():loop(type)
end

function Set_FM_SLJHBuff()
  CreateAutoCraftInstance():SetSLJHBuff()
end
-- /dump GetRemainRegents()
function GetRemainRegents()
  CreateAutoCraftInstance():GetRemainRegents(true)
end

function SetAvailableEnchantingRecipe()
  CreateAutoCraftInstance():SetAvailableEnchantingRecipe()
  print("SetAvailableEnchantingRecipe", CreateAutoCraftInstance().CurrentEnchantingRecipeID)
end

--切换到订单tab第三个
--/run local children = {ProfessionsFrame.TabSystem:GetChildren()} local third = children[3] if third then third:Click() end

